package cn.gailvlun.gll.config;

import android.databinding.BindingAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.ConvertUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.widget.CondensePlayView;
import cn.gailvlun.gll.widget.CountEditView;
import cn.gailvlun.gll.widget.OnPlayListener;
import cn.gailvlun.gll.widget.PlayButton;



public class AppBindingAdapter {
    @BindingAdapter("selectAvatar")
    public static void selectAvatar(ImageView imageView, String url) {
        if (imageView != null) {
            if (url == null) {
                url = "https://img.xiaogaijun.com/avatar_whiterabbit_neuter.jpg?x-oss-process=image/resize,m_lfit,h_300,w_300";
            }
            RequestOptions requestOptions = RequestOptions
                    .placeholderOf(R.drawable.ic_choose_image);

            Glide.with(imageView.getContext())
                    .load(url)
                    .apply(requestOptions)
                    .into(imageView);
        }
    }

    @BindingAdapter("android:src")
    public static void setImageResource(ImageView imageView, int resource) {
        imageView.setImageResource(resource);
    }

    @BindingAdapter("setWidthMinusPadding")
    public static void setWidth(View view, float dp) {
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.width = view.getContext().getResources().getDisplayMetrics().widthPixels - ConvertUtils.dp2px(dp) * 2;
    }

    @BindingAdapter("setUrl")
    public static void setUrl(CondensePlayView cpv, File file) {
        if (cpv != null && file != null) {
            cpv.setUrl(file.getPath());
        }
    }

    @BindingAdapter("onPlayChanged")
    public static void setOnPlay(PlayButton pb, OnPlayListener listener) {
        pb.setMOnPlayListener(listener);
    }

    



    @BindingAdapter("app:hint")
    public static void hint(CountEditView editView, CharSequence hint) {
        editView.setHint(hint);
    }
}